/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.sql.SQLException;
import java.sql.Statement;
import java.util.Hashtable;

import blueprint4j.utils.Log;
import blueprint4j.utils.ThreadScheduable;
import blueprint4j.utils.ThreadSchedule;

/**
 * This class stores only index fields
 */
public class IndexTable implements ThreadScheduable,java.io.Serializable  {
    
    public static final String TABLE_NAME = "d_index_table";
//	public static final String TABLE_NAME = "g_index_table";
    //private static boolean checked_table = false;
    private static Hashtable checked_table = new Hashtable();
    private static Object sync = new Object();
    
    static {
        ThreadSchedule.add(new IndexTable());
    }
    
    public  IndexTable() {}
    
    private static void createTableIfNotExist(DBConnection connection) {
        try {
            Statement st = DBTools.getLocalConnection(connection).createStatement();
            try {
                st.executeQuery(Entity.limitSQL(connection,"select * from "+TABLE_NAME,1));
            } catch (SQLException s) {
                connection.getDataBaseUtils().createIndexTable(st);
            }
            st.close();
        } catch (Exception exception) {
            exception.printStackTrace();
            Log.critical.out(exception);
            System.exit(-1);
        }
    }
    
    public static synchronized Long getNextIndex(Entity entity)
    throws SQLException,DataException {
        //synchronized (sync) {
        if (!checked_table.containsKey(entity.getConnection().getUrl())) {
            createTableIfNotExist(entity.getConnection());
            checked_table.put(entity.getConnection().getUrl(),entity.getConnection());
        }
        //}
        return entity.getDataBaseUtils().getNextIndexField(entity);
    }
    
    public boolean keepAlive() {return true;}
    public int sleepTime() {return 1000*60*60*24;}
    public void process()
    throws Exception {
 /* Dags - not used for the same purpose anymore
  *       Enumeration en = checked_table.elements();
        for (;en.hasMoreElements();) {
            DBConnection dbcon = DBTools.getLocalConnection((DBConnection)en.nextElement());
            Statement smt = dbcon.createStatement();
            ResultSet rs = smt.executeQuery(Entity.limitSQL(dbcon,"select index_field from " + TABLE_NAME +" order by index_field desc",1));
            if (rs.next()) {
                Log.trace.out("UPDATE DATABASE","delete from " + TABLE_NAME + " where index_field < " + Entity.asSQL(rs.getString(1)));
                smt.execute("delete from " + TABLE_NAME + " where index_field < " + Entity.asSQL(rs.getString(1)));
            }
            // now optimize table
            dbcon.getDataBaseUtils().executeOptimizeTable(dbcon,TABLE_NAME);
            smt.close();
        */
    }
    public void close() throws Exception {}
}
